﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Data.Linq;
using QFlow.Data;
using QFlow.Core;

namespace QFlow.Windows.Forms
{
    /// <summary>
    /// Login form
    /// </summary>
    public partial class Login : QFlow.Utils.BaseForm
    {
        bool _SwitchUser;

        /// <summary>
        /// Initializes a new instance of the <see cref="Login"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="switchUser">if set to <c>true</c> [switch user].</param>
        public Login(QFlowDbContext context, bool switchUser)
            : base(context)
        {
            InitializeComponent();
            this.Office2007ColorTable = DevComponents.DotNetBar.Rendering.eOffice2007ColorScheme.VistaGlass;
            _SwitchUser = switchUser;
        }

        /// <summary>
        /// Handles the Load event of the Login control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Login_Load(object sender, EventArgs e)
        {
            cmbUser.DataSource = this.Context.Users.ToArray();
        }

        /// <summary>
        /// Handles the Click event of the buttonX1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (cmbUser.SelectedIndex != -1)
            {
                this.lblError.Visible = false;
                User user;
                if (this.Context.Users.AuthenticateUser(this.cmbUser.Text, this.txtPassword.Text, out user))
                {
                    if (_SwitchUser)
                        LoggingManager.SwitchUsers(user);
                    else LoggingManager.LoginUser(user);
                    Authentication.SetAuthenticationToken(user);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.lblError.Visible = true;
                }
            }
            else
            {
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (_SwitchUser)
                this.Close();
            else Application.Exit();
        }

        protected override void OnActivated(EventArgs e)
        {
            
        }
    }
}
